<?php
/**
 * 轿辰会通知接口类
 * Created by www.soe-soe.com
 * Author: shijy
 * Date: 2016/7/7
 * Time: 11:18
 */

namespace Common\Model\OtherSystem;

use Common\Model\LogModel;
use Common\Model\UtilsModel;

class czk extends NoticeOtherSystem
{
    const JIAOCH_KEY='hzxhbyg4gjjyyqqkkdgzsmshjgzayjjj1000hzgdbuytszs800hb';

    private $noticeUrl='';


    public function __construct()
    {
        $this->noticeUrl=CZK_CENTER;
    }

    /**
     * cli方式请求数据
     * Author: shijy
     * @param $actionName
     * @param $param
     * @return bool|mixed
     *//*
    public function notice_cli($actionName,$param)
    {
        return false;
        ksort($param);
        $paramStr=http_build_query($param);
        $now=time();

        $sign=$this->mkSign($now,$paramStr);
        $paramStr .="&T={$now}&S={$sign}";
        $paramStr=urlencode($paramStr);
        exec("/usr/bin/php  ".WEB_ROOT."/cliApi.php Api/Jch/{$actionName} {$paramStr}",$output,$status);
        if($status!=0){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,0,NoticeOtherSystem::STATUS_FAILURE_SYSTEM);
            return false;
        }
        $resultStr  =implode($output);
        $resultObj  =json_decode($resultStr,true);
        if(!$resultObj || $resultObj['status']!=0){
                $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_FAILURE_FEEDBACK);
                return false;
        }
        $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_SUCCESS);
        return $resultObj;
    }
*/

    /**
     * 格式化参数，对数值型转为字符型
     * Author: shijy
     * @param $param
     * @return mixed
     */
    private function format_param($param){
        foreach($param as $k=>$v){
            if(is_numeric($v)){
                $param[$k]=$v.'';
            }elseif(is_array($v) || is_object($v)){
                $param[$k]=$this->format_param($v);
            }
        }
        return $param;
    }

    /**
     * cgi方式请求数据
     * Author: shijy
     * @param $actionName
     * @param $param
     * @return bool|mixed
     */
    public function notice_cgi($actionName,$param=null)
    {
        $param=$this->format_param($param);
        $now=time();
        $sign=$this->mkSign($actionName,$now,$param);
        $url=$this->noticeUrl.'act='.$actionName.'&sign='.$sign.'&t='.$now;
      //  LogModel::dailyLog($actionName,$url);
      //  LogModel::dailyLog($actionName,$param);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(is_null($param)){
            curl_setopt($ch, CURLOPT_HEADER, 0);
        }else{
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array('data'=>json_encode($param)));
        }

        $resultStr = curl_exec($ch);
        //$resultObj=json_decode($resultStr,true);
//        LogModel::dailyLog($actionName,$resultStr);
//
//
//
//        $no=curl_errno($ch);
//        $str=curl_error($ch);
//        LogModel::dailyLog($actionName,'错误');
//        LogModel::dailyLog($actionName,$no);


        if(is_null($resultStr)){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,0,NoticeOtherSystem::STATUS_FAILURE_SYSTEM);
            return false;
        }

        $resultObj=json_decode($resultStr,true);
        LogModel::dailyLog($actionName,$resultStr);

        if(!$resultObj || $resultObj['status']!=0){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_FAILURE_FEEDBACK);
            return false;
        }
        $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_SUCCESS);
        return $resultObj;
    }

    /**
     * sign算法
     * Author: shijy
     * @param $actionName 行为名称
     * @param $timestamp 时间戳
     * @param $param  将被post的数据
     * @return string sign签名
     */
    public function mkSign($actionName,$timestamp,$param=null){
        $temp=array();
        $temp['sk']    = self::JIAOCH_KEY;
        $temp['act']   = $actionName;
        $temp['t']     = $timestamp;
        if(!is_null($param)){
            $weidu=1;
            foreach($param as $k=>$v){
                if(is_array($v) || is_object($v)){
                    $weidu++;
                    break;
                }
            }
            if($weidu>1){
                $param=array('data'=>json_encode($param));
            }
            $temp['data']  =md5(json_encode($param));
        }
        ksort($temp);
        $paramStr=http_build_query($temp);
         return md5($paramStr);
    }

     /**
     * 轿辰会行为与星悦行为转换
     * Author: shijy
     * @param $JiaoCHAction
     * @return int
     */
    public static function actionSwitch_Jch_StarJoy($JiaoCHAction){
            switch($JiaoCHAction+0){
                case 1://"订单支付奖励";
                    $action=OPT_JCH_PAY;
                    break;
                case 2: //"订单评价奖励";
                    $action=OPT_JCH_COMMENT;
                    break;
                case 3: //"预约奖励";
                    $action=OPT_JCH_BESPEAK;
                    break;
                case 5://"新车评价奖励"
                    $action=OPT_JCH_NEWCAR;
                    break;
                case 100001: //"积分兑换";
                    $action=OPT_JCH_EXCHANGE;
                    break;
                case 100002: //"后台操作";
                    $action=OPT_JCH_ADMIN;
                    break;
                case 100003: //"会员活动";
                    $action=OPT_JCH_ACTIVE;
                    break;
                case 100004: //"账户合并";
                    $action=OPT_JCH_MERGER;
                    break;
                default:
                    return 0;
            }
        return $action;
        }


    
}